package cn.edu.ncu.housinghub.controller.landlord;

import cn.edu.ncu.housinghub.constant.ErrorCodeConstant;
import cn.edu.ncu.housinghub.dto.request.house.HouseConditionRequest;
import cn.edu.ncu.housinghub.dto.request.house.NearbyHouseRequest;
import cn.edu.ncu.housinghub.dto.response.house.PageResult;
import cn.edu.ncu.housinghub.entity.house.House;
import cn.edu.ncu.housinghub.exception.BusinessException;
import cn.edu.ncu.housinghub.service.HouseService;
import cn.edu.ncu.housinghub.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 房产认证、维护
 */
@RestController
@RequestMapping("/house")
public class HouseController {
    @Autowired
    private HouseService houseService;

    /**
     * 获取所有房产
     */
    @GetMapping("/selectAll")
    public List<House> getAllHouses() {
        return houseService.selectAllHouses();
    }

    @PostMapping("/selectByCondition")
    public List<House> selectHousesByCondition(@RequestBody HouseConditionRequest condition) {
        return houseService.selectHousesByCondition(condition);
    }

    @PostMapping("/selectNearby")
    public List<House> selectNearbyHouses(@RequestBody NearbyHouseRequest request) {
        // 参数验证，方法在下面的private
        validateNearbyRequest(request);
        return houseService.selectNearbyHouses(request);
    }

    /**
     * 验证请求参数,在selectNearbyHouses中调用
     */
    private void validateNearbyRequest(NearbyHouseRequest request) {
        if (request.getLongitude() == null || request.getLatitude() == null) {
            throw new BusinessException(ErrorCodeConstant.PARAM_VALIDATION_FAILED);
        }

        if (request.getLongitude().compareTo(BigDecimal.valueOf(-180)) < 0 ||
                request.getLongitude().compareTo(BigDecimal.valueOf(180)) > 0) {
            throw new BusinessException(ErrorCodeConstant.PARAM_VALIDATION_FAILED);
        }

        if (request.getLatitude().compareTo(BigDecimal.valueOf(-90)) < 0 ||
                request.getLatitude().compareTo(BigDecimal.valueOf(90)) > 0) {
            throw new IllegalArgumentException("纬度必须在-90到90之间");
        }

        if (request.getRadius() == null) {
            request.setRadius(5000); // 默认5公里
        } else if (request.getRadius() <= 0 || request.getRadius() > 50000) {
            throw new IllegalArgumentException("搜索半径必须在1-50000米之间");
        }
    }

    @PostMapping("/selectNearbyWithPage")
    public PageResult<House> selectNearbyHousesWithPage(@RequestBody NearbyHouseRequest request) {
        // 参数验证
        validateNearbyRequest(request);
        return houseService.selectNearbyHousesWithPage(request);
    }

    @GetMapping("/{houseId}")
    public Result getHouseDetail(@PathVariable Long houseId) {
        try {
            House house = houseService.getHouseDetailById(houseId);
            if (house != null) {
                return Result.success(ResponseEntity.ok(house));
            } else {
                return Result.error(ErrorCodeConstant.HOUSE_NOT_EXIST.getCode(), ErrorCodeConstant.HOUSE_NOT_EXIST.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ErrorCodeConstant.GET_HOUSE_DETAIL_ERROR.getCode(), e.getMessage());
        }
    }
}
